import useStatus from "./useStatus";
import useUpdateStatus from "./useUpdateStatus";
import useRemove from "./useRemove";

import { statusOptions } from "@/const";

export default ({
  renderModalForm,
  updateStatus,
  remove,
  actionRef,
  renderAction,
  insertPosition = 2,
  renderOtherAction,
  actionColumnWidth,
  scroll,
  onlyAction = false,
  hiddenAction = false,
}) => {
  const myRenderAction = (_, record) => {
    const arr = [
      renderModalForm(2, record),
      updateStatus && useUpdateStatus(updateStatus, record, actionRef),
      remove && useRemove(remove, record, actionRef),
    ];
    if (renderOtherAction) {
      const result = renderOtherAction(_, record);
      if (Array.isArray(result)) {
        arr.splice(insertPosition, 0, ...result);
      } else {
        arr.splice(insertPosition, 0, result);
      }
    }
    if (renderAction) {
      return renderAction(_, record, renderModalForm, actionRef);
    } else {
      return <>{arr.map((item) => item)}</>;
    }
  };

  const columns = [
    {
      title: "登记人",
      dataIndex: "operatorName",
    },
    {
      title: "登记时间",
      dataIndex: "modidate",
      valueType: "dateTimeRange",
      render: (_, record) => {
        return record.modidate;
      },
    },
    {
      title: "状态",
      dataIndex: "status",
      valueType: "select",
      fieldProps: {
        options: statusOptions,
      },
      render: useStatus,
    },
    {
      title: "操作",
      fixed: scroll ? "right" : false,
      dataIndex: "option",
      valueType: "option",
      width: actionColumnWidth || 200,
      render: myRenderAction,
    },
  ];

  // 没有启用/禁用方法，则不显示启用/禁用和状态列
  if (!updateStatus) {
    columns.splice(2, 1);
  }

  // 隐藏操作列
  if (hiddenAction) {
    columns.splice(-1, 1);
  }
  return onlyAction ? columns.slice(-1) : columns;
};
